#define _CRT_SECURE_NO_WARNINGS
#include"binarytree.h"

void QueueInit(Queue* qp)
{
	assert(qp);
	qp->head = NULL;
	qp->tail = NULL;
	qp->size = 0;
}

void QueueDestory(Queue* qp)
{
	assert(qp);
	QueueNode* cur = qp->head;
	while (cur)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
}

void QueuePush(Queue* qp, Qdatetype x)
{
	assert(qp);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		perror("malloc");
		return;
	}
	newnode->date = x;
	newnode->next = NULL;
	if (qp->head == NULL)
	{
		qp->head = qp->tail = newnode;
	}
	else
	{
		qp->tail->next = newnode;
		qp->tail = qp->tail->next;
	}
	qp->size++;
}

void QueuePop(Queue* qp)
{
	assert(qp);
	assert(qp->head);
	if (qp->head->next == NULL)
	{
		free(qp->head);
		qp->head = NULL;
		qp->size--;
	}
	else
	{
		QueueNode* newhead = qp->head->next;
		free(qp->head);
		qp->head = newhead;
		qp->size--;
	}
}

int QueueSize(Queue* qp)
{
	assert(qp);
	return qp->size;
}

int QueueEmpty(Queue* qp)
{
	if (qp->size == 0)return 1;
	else return 0;
}

Qdatetype QueueFront(Queue* qp)
{
	return qp->head->date;
}

Qdatetype QueueBack(Queue* qp)
{
	return qp->tail->date;
}